//@author A0081229H
/*
 *  testinputs.txt and expectedoutput.txt also written by me
 */
package systemTesting;

import logic.Logic;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.LinkedList;

public class SystemTester {
	private static String inputFile;
	private static String outputFile;
	Logic logic;
	LinkedList<String> expectedOutput;

	public SystemTester(String input, String output) {
		inputFile = input;
		outputFile = output;
		logic = new Logic();
		expectedOutput = new LinkedList<String>();
	}

	private void runTest() throws IOException {
		int total = 0, pass = 0;
		File f = new File(inputFile);
		FileReader fr = new FileReader(f);
		BufferedReader br = new BufferedReader(fr);
		String newLine = "";
		int paragraph = 0;
		while ((newLine = br.readLine()) != null) {
			// counters
			total++;
			// feed line into TASKEY
			logic.executeCommand(newLine);
			// process expected output
			processExpectedOutput(paragraph);
			paragraph++;
			// check expected output against real output
			// output results for each line
			File realOutput = new File("file.txt");
			BufferedReader rdReal = new BufferedReader(new FileReader(realOutput));
			String lnReal = rdReal.readLine();
			boolean similar = true;
			String failureMsg = null;
			while (!expectedOutput.isEmpty() ||
					(lnReal != null)) {
				String lnExpected = expectedOutput.poll();
				if (lnReal == null || lnExpected == null) {
					similar = false;
					failureMsg = "FC #"+paragraph+": (Expected)\""+lnExpected+"\" vs (Real)\""+lnReal+"\"";
				} else if (lnExpected.compareTo(lnReal) != 0) {
					similar = false;
					failureMsg = "FC #"+paragraph+": (Expected)\""+lnExpected+"\" vs (Real)\""+lnReal+"\"";
				}

				lnReal = rdReal.readLine();
			}
			String result;
			if (similar) {
				result = "Passed.";
				pass++;
			} else {
				result = "Failed";
			}
			if (failureMsg == null) {
				System.out.println("Test "+paragraph+": "+result+"("+newLine+")");
			} else {
				System.out.println("-----------------------------------------");
				System.out.println("Test "+paragraph+": "+result+"("+newLine+")");
				System.out.println(failureMsg);
				System.out.println("-----------------------------------------");
			}


			expectedOutput.clear();
		}
		br.close();
		double success_perc = ((double) pass / (double) total) * 100;
		System.out.println("Success Rate: "+pass+"/"+total+" ("+success_perc+"%)");
		logic.executeCommand("exit");
	}

	private void processExpectedOutput(int i) throws IOException {
		File f = new File(outputFile);
		FileReader fr = new FileReader(f);
		BufferedReader br = new BufferedReader(fr);
		String newLine = "";
		int paragraph = 0;
		while (paragraph != i) {
			newLine = br.readLine();
			if (newLine.equals("NEXT")) {
				paragraph++;
			}
		}
		newLine = br.readLine();
		while (!newLine.equals("NEXT")) {
			expectedOutput.add(newLine);
			newLine = br.readLine();
		}
		br.close();

	}


	public static void main(String[] args) throws IOException {
		SystemTester st = new SystemTester(args[0], args[1]);
		st.runTest();
		System.exit(0);
	}

}
